package PowerWord.Core.Plan;

import java.util.ArrayList;

import PowerWord.Core.ReciteEffect.WordProperty;

public class Exercise {

	Duty mDuty = null;
	int score = 0;
	int GroupFullSize = 2;
	int GroupCurSize = 0;
	int curGroup = 0;
	int maxGroup = 0;

	ArrayList<Integer> wrongAnswers = new ArrayList<Integer>();
	
	public Exercise(int groupSize){
		GroupFullSize = groupSize;
		mDuty = Controller.Instance().GetDuty();
		maxGroup = mDuty.GetCount()/GroupFullSize + (mDuty.GetCount()%GroupFullSize == 0 ? 0 : 1);
	}
	
	public void SetGroupSize(int n){
		if(n > 0){
			GroupFullSize = n;
			maxGroup = mDuty.GetCount()/GroupFullSize + (mDuty.GetCount()%GroupFullSize == 0 ? 0 : 1);
		}
	}
	
	public int GetCurGroupSize(){
		return (curGroup <= (maxGroup - 1)) ? GroupFullSize : mDuty.GetCount() % GroupFullSize;
	}
	
	public int GetTotalGroup(){
		return maxGroup;
	}
	
	public int GetCurrentGroup(){
		return curGroup;
	}
	
	public int GotoNextGroup(){
		++curGroup;
		if(curGroup < maxGroup - 1){
			return 1;
		}
		else if(curGroup < maxGroup){
			return 0;
		}
		else{
			return -1;
		}
	}
	
	public void Reset(){
		curGroup = 0;
		score = 0;
	}
	
	public double GetScore(){
		return score * 100.f / mDuty.GetCount();
	}
	
	public void PassWrongToDuty(){
		mDuty.subList = new Integer[wrongAnswers.size()];
		wrongAnswers.toArray(mDuty.subList);
	}
	
	public void Random(){
		mDuty.Random();
	}
	
	public void Order(){
		mDuty.Order();
	}
	
	protected void AddWrongWord(int index){
		int wrongAnswerId = curGroup * GroupFullSize + index;
		wrongAnswers.add(mDuty.subList[wrongAnswerId]);
		Controller.Instance().SetWordProperty(wrongAnswerId, WordProperty.Wrong);
	}

	public void Prepare() {
	}
}
